// 引入接口文件
import { getRegion,getProject,updateStatus,exportExcel} from './integralExchange_cloudServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import Cookie from 'js-cookie'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
// 导入项目全局配置文件
import config from 'config/config'
import { makeAddress ,download} from "utils/util"
// 初始默认状态
const defultState = {
  biz: {
		queryForm: {}
  },
  regionList:[],
  projectList:[],
  modalObj:{
    visible:false
	},
	exchangeDetail:{}
}

// 声明module
const tmpModule = {
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: `getProject`,payload:{}});
          dispatch({type: `getRegion`,payload: {regionIds:[Cookie.get(config.cookie.regionId)?Cookie.get(config.cookie.regionId):'A30003439']}});
          
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    *getRegion({ payload }, { put, call, select }) {
      try {
        let {success,rows} = yield call(getRegion, {...payload })
        if (success) {
          let regionList = rows
            regionList.forEach((item,index)=>{
      				item.value = item.regionId
      				item.label = item.regionName
      				item.key = index
            })
          yield put({ type: 'updateStore', payload: { regionList } })
        }
      } catch (e) {
      }
    },
    *getProject({ payload }, { put, call, select }) {
      try {
        let {success,rows} = yield call(getProject, {...payload })
        if (success) {
          let projectList = rows
          projectList.forEach((item,index)=>{
      				item.value = item.productId
      				item.label = item.name
      				item.key = index
            })
          yield put({ type: 'updateStore', payload: { projectList } })
        }
      } catch (e) {
      }
    },
    *updateStatus({ payload }, { put, call, select }){
      try{
        
        let {success} = yield call(updateStatus, {...payload });
        if(success){
          let {biz} = yield select(state => state[tmpModule.namespace])
          biz.refreshListDatas(true)
        }
      }catch(e){

      }
    },
    *exportExcel({ payload }, { put, call, select }) {
      let params = localStorage.regionId&&localStorage.regionId != 'undefined'?localStorage.regionId:'A30003439'
      let {success,data} = yield call(exportExcel, params)
      if (success) {
        download(data)
      }
  },
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
